package com.pingan.cn.route;

import com.pingan.cn.common.utils.ResponseUtil;
import com.pingan.cn.common.utils.SpringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Transactional
@Service("routeUserService")
public class RouteUserService {
    @Autowired
    private RouteUserDao routeUserDao;

    public void createDefaultData() {
        if (routeUserDao.count() == 0) {
            RouteUser defaultUser = new RouteUser();
            defaultUser.setUsername("admin");
            defaultUser.setPassword("123456");
            defaultUser.setRole("admin");
            routeUserDao.save(defaultUser);
        }
    }

    //注册
    public ResponseUtil register(RouteUser user){
        try {
            RouteUser result = routeUserDao.saveAndFlush(user);
            return ResponseUtil.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }
    }

    //登录查找
    public ResponseUtil findByUsernameAndPassword(String username, String password){
        try {
            RouteUser result = routeUserDao.findByUsernameAndPassword(username,password);
            return ResponseUtil.success(result);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }
    }

    //查询所有
    public ResponseUtil findAll(){
        try {
            List<RouteUser> users = routeUserDao.findAll();
            return ResponseUtil.success(users);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }
    }

    public ResponseUtil deleteById(String id){
        try {
            routeUserDao.deleteById(id);
            return ResponseUtil.success();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }
    }

//    批量删除
    public boolean deleteBatch(String[] ids){
        ArrayList<RouteUser> arrayList = new ArrayList<>();
        for (String id:ids) {
            RouteUser entity = findById(id);
            if (entity != null){
                arrayList.add(entity);
            }
        }
        try {
            routeUserDao.deleteInBatch(arrayList);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public ResponseUtil findUserById(String id){
        try {
            RouteUser user =  routeUserDao.findById(id).get();
            return ResponseUtil.success(user);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.error(e.getMessage());
        }
    }

    public RouteUser findById(String id){
        return routeUserDao.findById(id).get();
    }

    public boolean update(RouteUser action){
        if(action == null || action.getId() == null){
            return  false;
        }
        Optional<RouteUser> byId = routeUserDao.findById(action.getId());
        if (byId.isPresent()){
            RouteUser oldAction = routeUserDao.findById(action.getId()).get();
            BeanUtils.copyProperties(action,oldAction, SpringUtil.getNullPropertyNames(action));
            try {
                routeUserDao.saveAndFlush(oldAction);
                return true;
            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }
}
